package hm.edu.cs;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BookListAdapter extends BaseAdapter{

	private LayoutInflater mInflater;
	private BookList list;


	public BookListAdapter(BookList bookList, Context context) 
	{
		list = bookList;
		mInflater = LayoutInflater.from(context);	
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Book getItem(int position) {
		return list.getBook(position);
	}

	@Override
	public long getItemId(int position) {
		if (list.getBook(position) != null) {
			return position;
		}
		return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;

		if (convertView == null) {

			convertView = mInflater.inflate(R.layout.book_listview, null);

			holder = new ViewHolder();

			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.status = (ImageView) convertView.findViewById(R.id.icon);

			convertView.setTag(holder);
		} else {

			holder = (ViewHolder) convertView.getTag();
		}
		Book item = list.getBook(position);

		holder.title.setText(item.getTitle());

		if (item.getStatusfarbe().equals("rot")) {

			holder.status.setImageResource(R.drawable.ic_rot);
		}

		else if (item.getStatusfarbe().equals("gruen"))  {
			holder.status.setImageResource(R.drawable.ic_gruen);
		}

		return convertView;
	}

	class ViewHolder {

		TextView title;
		ImageView status;
	}

}
